﻿using Sirian.Serialization;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Reflection;
using System.IO;
using GlobalGameClasses;
using Serialization;
using System.Collections.Generic;
using Microsoft.Xna.Framework;

namespace InSovietRussiaProgrammTestsYou
{
    
    
    /// <summary>
    ///This is a test class for LogWriterTest and is intended
    ///to contain all LogWriterTest Unit Tests
    ///</summary>
    [TestClass()]
    public class LogWriterTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        [ClassCleanup()]
        public static void MyClassCleanup()
        {
            GlobalGameValues.Values.achievementsUnlocked = new List<Achievement>();
            GlobalGameValues.Values.content = new ContentCollection();
            GlobalGameValues.Values.difficulty = null;
            GlobalGameValues.Values.errorWriter = null;
            GlobalGameValues.Values.gameTime = new GameTime();
            GlobalGameValues.Values.GlobalStrings = new StringContainer();
            GlobalGameValues.Values.graphicsDevice = null;
            GlobalGameValues.Values.score = new InterfaceComponents.HUDWrapper<int>(new List<InterfaceComponents.HUDComponent>());
            GlobalGameValues.Values.soundPlayer = null;
            GlobalGameValues.Values.spriteBatch = null;
            GlobalGameValues.Values.volume_Music = 0;
            GlobalGameValues.Values.volume_soundEffect = 0;
        }
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for writeError
        ///</summary>
        [TestMethod()]
        public void writeErrorTest()
        {
            string errorLogPath = System.IO.Path.GetDirectoryName(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)) + "test.log";
            clearLog(errorLogPath);
            LogWriter target = new LogWriter(errorLogPath); 
            string errorMessage = "TEST";
            bool result;
            result = target.writeError(errorMessage);
            Assert.IsTrue(result);

            using (StreamReader sr = new StreamReader(errorLogPath))
            {
                string input = sr.ReadToEnd();
                string[] lines = input.Split('\n');
                Assert.AreEqual(lines.Length, 2);
                Assert.IsTrue(lines[0].Substring(lines[0].Length - errorMessage.Length - 1, errorMessage.Length) ==  errorMessage);
                sr.Close();
            }

            target = new LogWriter(errorLogPath + "/)H§");
            result = target.writeError(errorMessage);
            Assert.IsFalse(result);
        }

        private void clearLog(string logpath)
        {
            using (StreamWriter sw = new StreamWriter(logpath, false))
            {
                sw.Write("");
                sw.Close();
            }
        }
    }
}
